package com.mycontract.suitepro.components.bar

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.mycontract.suitepro.R
import com.mycontract.suitepro.contract.ContractIndexActivity
import com.mycontract.suitepro.databinding.ViewContractSuiteTopNavigationBarBinding
import com.mycontract.suitepro.extend.getContractSuiteParentContractSuiteActivity
import com.mycontract.suitepro.extend.startContractSuiteToContractSuiteNextContractSuiteActivity
import com.mycontract.suitepro.extend.startContractSuiteToContractSuiteNextContractSuiteActivityContractSuiteForContractSuiteSingleContractSuiteTop
import com.mycontract.suitepro.contract.LaunchActivity

/**
<declare-styleable name="ContractSuiteTopNavigationBar">
<attr name="cwtnbCenterTextColor" format="color" />
<attr name="cwtnbCenterText" format="string" />
<attr name="cwtnbLeftImageRes" format="reference" />
<attr name="cwtnbRightImageRes" format="reference" />
<attr name="cwtnbLeftImageHidden" format="boolean" />
<attr name="cwtnbLeftTitleHidden" format="boolean" />
<attr name="cwtnbLeftTitleColor" format="color" />
<attr name="cwtnbRightImageHidden" format="boolean" />
<attr name="cwtnbLeftActionMode" format="integer" />
<attr name="cwtnbRightActionMode" format="integer" />
</declare-styleable>
 * */
class TopNavigationBar : RelativeLayout {

    lateinit var mContractSuiteViewContractSuiteBinding: ViewContractSuiteTopNavigationBarBinding
    private lateinit var vContractSuiteRootContractSuiteView: View

    private var cwtnbCenterTextColor: Int = 0
    private var cwtnbCenterText: String? = null
    private var cwtnbLeftTitle: String? = null
    private var cwtnbLeftTitleColor: Int = 0
    private var cwtnbLeftImageRes: Int = 0
    private var cwtnbRightImageRes: Int = 0
    private var cwtnbRightImageHidden: Boolean = true
    private var cwtnbLeftImageHidden: Boolean = false
    private var cwtnbLeftTitleHidden: Boolean = false
    private var cwtnbLeftActionMode: Int = 0
    private var cwtnbRightActionMode: Int = 0
    private var cwtnbBottomLineHidden: Boolean = false

    private var mContractSuiteSupportContractSuiteFragmentContractSuiteManager: FragmentManager? = null

    private var mContractSuiteOnContractSuiteActionContractSuiteClickContractSuiteListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(attrs, R.styleable.TopNavigationBar
                )
                cwtnbCenterTextColor = array.getColor(
                    R.styleable.TopNavigationBar_cwtnbCenterTextColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwtnbCenterText =
                    array.getString(R.styleable.TopNavigationBar_cwtnbCenterText)

                cwtnbLeftTitle =
                    array.getString(R.styleable.TopNavigationBar_cwtnbLeftTitle)
                cwtnbLeftImageRes =
                    array.getResourceId(R.styleable.TopNavigationBar_cwtnbLeftImageRes, 0)
                cwtnbLeftImageHidden = array.getBoolean(
                    R.styleable.TopNavigationBar_cwtnbLeftImageHidden,
                    false
                )
                cwtnbLeftTitleHidden = array.getBoolean(
                    R.styleable.TopNavigationBar_cwtnbLeftTitleHidden,
                    false
                )
                cwtnbLeftTitleColor = array.getColor(
                    R.styleable.TopNavigationBar_cwtnbLeftTitleColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )

                cwtnbRightImageRes =
                    array.getResourceId(R.styleable.TopNavigationBar_cwtnbRightImageRes, 0)
                cwtnbRightImageHidden = array.getBoolean(
                    R.styleable.TopNavigationBar_cwtnbRightImageHidden,
                    false
                )

                cwtnbLeftActionMode = array.getInteger(
                    R.styleable.TopNavigationBar_cwtnbLeftActionMode,
                    resources.getInteger(R.integer.ActionModeFinish)
                )
                cwtnbRightActionMode = array.getInteger(
                    R.styleable.TopNavigationBar_cwtnbRightActionMode,
                    resources.getInteger(R.integer.ActionModeCustomerService)
                )

                cwtnbBottomLineHidden = array.getBoolean(
                    R.styleable.TopNavigationBar_cwtnbBottomLineHidden,
                    false
                )

                array.recycle()
            }
        }
        context?.let {
            vContractSuiteRootContractSuiteView = View.inflate(it, R.layout.view_contract_suite_top_navigation_bar, this)
            mContractSuiteViewContractSuiteBinding = ViewContractSuiteTopNavigationBarBinding.bind(vContractSuiteRootContractSuiteView)
            initContractSuiteSubviews()
        }
    }

    private fun initContractSuiteSubviews() {
        try {
            mContractSuiteViewContractSuiteBinding.textTopNavigationBarCenter.text = cwtnbCenterText
            mContractSuiteViewContractSuiteBinding.textTopNavigationBarCenter.setTextColor(cwtnbCenterTextColor)

            mContractSuiteViewContractSuiteBinding.imageTopNavigationBarLeft.setImageResource(cwtnbLeftImageRes)
            mContractSuiteViewContractSuiteBinding.imageTopNavigationBarRight.setImageResource(cwtnbRightImageRes)

            mContractSuiteViewContractSuiteBinding.textTopNavigationBarLeft.text = (cwtnbLeftTitle)
            mContractSuiteViewContractSuiteBinding.textTopNavigationBarLeft.setTextColor(cwtnbLeftTitleColor)

            if (cwtnbLeftImageHidden) {
                mContractSuiteViewContractSuiteBinding.imageTopNavigationBarLeft.visibility = View.GONE
            } else {
                mContractSuiteViewContractSuiteBinding.imageTopNavigationBarLeft.visibility = View.VISIBLE
            }

            if (cwtnbLeftTitleHidden) {
                mContractSuiteViewContractSuiteBinding.textTopNavigationBarLeft.visibility = View.GONE
            } else {
                mContractSuiteViewContractSuiteBinding.textTopNavigationBarLeft.visibility = View.VISIBLE
            }

            if (cwtnbRightImageHidden) {
                mContractSuiteViewContractSuiteBinding.imageTopNavigationBarRight.visibility = View.GONE
            } else {
                mContractSuiteViewContractSuiteBinding.imageTopNavigationBarRight.visibility = View.VISIBLE
            }

            if (cwtnbBottomLineHidden) {
                mContractSuiteViewContractSuiteBinding.vTopNavigationBarLine.visibility = View.GONE
            } else {
                mContractSuiteViewContractSuiteBinding.vTopNavigationBarLine.visibility = View.VISIBLE
            }

            mContractSuiteViewContractSuiteBinding.textTopNavigationBarLeft.setOnClickListener {
                setContractSuiteBarContractSuiteClickContractSuiteAction(isLeftAction = true)
            }

            mContractSuiteViewContractSuiteBinding.imageTopNavigationBarLeft.setOnClickListener {
                setContractSuiteBarContractSuiteClickContractSuiteAction(isLeftAction = true)
            }

            mContractSuiteViewContractSuiteBinding.imageTopNavigationBarRight.setOnClickListener {
                setContractSuiteBarContractSuiteClickContractSuiteAction(isLeftAction = false)
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    private fun setContractSuiteBarContractSuiteClickContractSuiteAction(isLeftAction: Boolean) {
        if (isLeftAction) {
            when (cwtnbLeftActionMode) {
                resources.getInteger(R.integer.ActionModeClick) -> {
                    mContractSuiteOnContractSuiteActionContractSuiteClickContractSuiteListener.invoke(R.integer.NavbarActionLeft)
                }
                resources.getInteger(R.integer.ActionModeFinish) -> {
                    val gActivity = getContractSuiteParentContractSuiteActivity()
                    gActivity?.finish()
                }
                resources.getInteger(R.integer.ActionModeToRoot) -> {
                    val gActivity = getContractSuiteParentContractSuiteActivity()
                    gActivity?.let { it1 ->
                        if (it1 is AppCompatActivity) {
                            it1.startContractSuiteToContractSuiteNextContractSuiteActivityContractSuiteForContractSuiteSingleContractSuiteTop<ContractIndexActivity>(HashMap<String, String>())
                        }
                    }
                }
                resources.getInteger(R.integer.ActionModeCustomerService) -> {
                    val gActivity = getContractSuiteParentContractSuiteActivity()
                    gActivity?.let {
//                        CustomerServiceDialog.showContractSuiteDialog(it.supportFragmentManager)
                    }
                }
            }
        } else {
            when (cwtnbRightActionMode) {
                resources.getInteger(R.integer.ActionModeClick) -> {
                    mContractSuiteOnContractSuiteActionContractSuiteClickContractSuiteListener.invoke(R.integer.NavbarActionRight)
                }
                resources.getInteger(R.integer.ActionModeFinish) -> {
                    val gActivity = getContractSuiteParentContractSuiteActivity()
                    gActivity?.finish()
                }
                resources.getInteger(R.integer.ActionModeToRoot) -> {
                    val gActivity = getContractSuiteParentContractSuiteActivity()
                    gActivity?.let { it1 ->
                        if (it1 is AppCompatActivity) {
                            it1.startContractSuiteToContractSuiteNextContractSuiteActivity<LaunchActivity>(HashMap<String, String>())
                        }
                    }
                }
                resources.getInteger(R.integer.ActionModeCustomerService) -> {
                    val gActivity = getContractSuiteParentContractSuiteActivity()
                    gActivity?.let {
//                        CustomerServiceDialog.showContractSuiteDialog(it.supportFragmentManager)
                    }
                }
            }
        }
    }

    fun setContractSuiteLeftContractSuiteText(leftText:String){
        cwtnbLeftTitle = leftText
        mContractSuiteViewContractSuiteBinding.textTopNavigationBarLeft.text = (cwtnbLeftTitle)
    }

    fun setContractSuiteCenterContractSuiteText(leftText:String){
        cwtnbCenterText = leftText
        mContractSuiteViewContractSuiteBinding.textTopNavigationBarCenter.text = (cwtnbCenterText)
    }

    fun setContractSuiteFragmentContractSuiteManager(manager: FragmentManager) {
        mContractSuiteSupportContractSuiteFragmentContractSuiteManager = manager
    }

    fun setContractSuiteOnContractSuiteActionContractSuiteClickContractSuiteListener(listener: (Int) -> Unit) {
        mContractSuiteOnContractSuiteActionContractSuiteClickContractSuiteListener = listener
    }
}